package cn.itsource.pet.service.impl;

import cn.itsource.basic.contants.SystemContants;
import cn.itsource.basic.domain.Point;
import cn.itsource.basic.service.impl.BasicServiceImpl;
import cn.itsource.basic.util.DistanceUtil;
import cn.itsource.basic.util.MD5Utils;
import cn.itsource.org.domain.Shop;
import cn.itsource.org.mapper.EmployeeMapper;
import cn.itsource.org.mapper.ShopMapper;
import cn.itsource.org.service.IShopService;
import cn.itsource.pet.domain.Pet;
import cn.itsource.pet.domain.PetDetail;
import cn.itsource.pet.domain.SearchMasterMsg;
import cn.itsource.pet.mapper.PetDetailMapper;
import cn.itsource.pet.mapper.PetMapper;
import cn.itsource.pet.service.ISearchMasterMsgService;
import cn.itsource.user.domain.LoginInfo;
import cn.itsource.user.domain.User;
import cn.itsource.user.mapper.LoginInfoMapper;
import cn.itsource.user.mapper.UserMapper;
import cn.itsource.user.service.IUserService;
import cn.itsource.user.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class SearchMasterMsgServiceImpl extends BasicServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {

    @Autowired
    private IShopService shopService;

    @Autowired
    private PetMapper petMapper;
    @Autowired
    private PetDetailMapper petDetailMapper;

    @Override
    public void add(SearchMasterMsg searchMasterMsg) {
        //计算离用户最近的门店

        //1.获取用户输入地址的 Point
        Point point = DistanceUtil.getPoint(searchMasterMsg.getAddress());

        //查询系统里面所有激活的店铺
        List<Shop> shops =  shopService.queryAllByState();
        //2.计算离用户最近的门店
        Shop shop = DistanceUtil.getNearestShop(point, shops);
        searchMasterMsg.setShop(shop);

        searchMasterMsg.setState(0);
        //保存寻主消息
        super.add(searchMasterMsg);
        //给shop 对应的employee 的手机号或者 邮箱发送提示信息

    }

    @Override
    @Transactional
    public void msgToPet(Long id) {
        //1.根据id 查询 寻主消息
        SearchMasterMsg searchMasterMsg = super.queryById(id);
        //2.更新searchMasterMsg 状态 改为 1
        searchMasterMsg.setState(1);
        //x修改当前寻主消息的状态
        super.update(searchMasterMsg);
        //3.searchMasterMsg 转换为一个 t_pet   保存一个 t_pet_detail
        Pet pet = new Pet();
        pet.setName(searchMasterMsg.getName());
        pet.setCreatetime(new Date());
        pet.setState(0);
        pet.setType_id(searchMasterMsg.getPet_type());
        Shop shop = new Shop();
        shop.setId(searchMasterMsg.getShop_id());
        pet.setShop(shop);
        pet.setSearchMasterMsg(searchMasterMsg);
        petMapper.save(pet);
        //设置宠物的详情
        PetDetail petDetail = new PetDetail();
        petDetail.setPet_id(pet.getId());
        petDetailMapper.save(petDetail);
    }
}
